package com.leapp.sk.ui.activity.personal

import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import cc.vv.lkbasecomponent.http.LKHttp
import cc.vv.lkbasecomponent.http.lib.setting.Settings
import cc.vv.lkbasecomponent.util.LKSPUtil
import cc.vv.lklibrary.http.base.okhttpfactory.plugins.httpplugin.MediaTypeWrap
import com.example.baselibrary.util.KEY_NICKNAME
import com.example.baselibrary.util.LKToastUtil
import com.example.mvp.activity.BaseActivityMVP
import com.example.mvp.binder.BaseDataBinder
import com.leapp.sk.R
import com.leapp.sk.app.UPDATE_PERSONAL_INFO
import com.leapp.sk.bean.request.login.UpdatePersonalInfoRequestEntity
import com.leapp.sk.bean.response.login.UpdatePersonalInfoResponseEntity
import com.leapp.sk.binder.BaseBinder
import com.leapp.sk.inter.TopBarRightClickInterface
import com.example.util.finallist.CODE_204
import com.example.util.finallist.SETTING_NICK
import com.example.util.finallist.SKCODE_200
import com.leapp.sk.viewdelegate.personal.SettingNickDelegate

/**
 * 设置昵称
 * @author  chaohaipeng  on 2018/8/8.
 */
class SettingNickActivity :BaseActivityMVP<SettingNickDelegate>(), TopBarRightClickInterface {
    override fun searchData() {
        httpUpdateData()
    }

    override fun initDate() {
        val nick = intent?.getStringExtra(SETTING_NICK)
        viewDelegate.initEditText(nick,this)
    }

    override fun bindEvenListener() {
    }

    override fun getDelegateClass(): Class<SettingNickDelegate> {
        return SettingNickDelegate::class.java
    }

    override fun getDataBinder(): BaseDataBinder<*, *> {
        return BaseBinder()
    }

    override fun initView(p0: Bundle?) {
        viewDelegate.setTitle(resources.getString(R.string.string_setting_nick))

    }
    /**
     * 更新数据
     */
    private fun httpUpdateData() {
        val updatePersonalInfoRequestEntity = UpdatePersonalInfoRequestEntity()
        updatePersonalInfoRequestEntity.nickName=viewDelegate.getSubmitContent()
        LKHttp.post(UPDATE_PERSONAL_INFO, updatePersonalInfoRequestEntity, UpdatePersonalInfoResponseEntity::class.java, object : BaseCallBack<UpdatePersonalInfoResponseEntity>(this) {
            override fun onSuccess(url: String?, resultText: String?, obj: UpdatePersonalInfoResponseEntity?) {
                super.onSuccess(url, resultText, obj)
                if (obj?.code == SKCODE_200) {
                    val submitContent = viewDelegate?.getSubmitContent()
                    if (!TextUtils.isEmpty(submitContent)){
                        LKSPUtil.getInstance().put(KEY_NICKNAME,submitContent!!)
                    }
                    val intent: Intent = Intent(this@SettingNickActivity, PersonalDataActivity::class.java)
                    intent.putExtra(SETTING_NICK,viewDelegate.getSubmitContent())
                    this@SettingNickActivity.setResult(CODE_204,intent)
                    this@SettingNickActivity.finish()
                } else {
                    LKToastUtil.showToastShort(obj?.message)
                }
            }

            override fun onFailure(url: String?, isLoading: Boolean, exceptionStr: String?) {
                super.onFailure(url, isLoading, exceptionStr)
                LKToastUtil.showToastShort(resources.getString(R.string.app_net_erro))
            }
        }
                , true, Settings(MediaTypeWrap.MEDIA_TYPE_FORM))

    }
}